<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\PrefeModel;
use Think;
use \sinacloud\sae\Storage as Storage;
//优惠管理类
class PrefeController extends Controller {
	//查看优惠
	public function showprefe(){
		//要跳转到第几页
		$startPage=I('get.p')?I('get.p'):1;
		//每页显示多少条
		$page->listRows=5;
		//从第几条开始显示
		$page->firstRow = $startPage*$page->listRows;


		$buss_id = session("admin_id");
		$mp = M("prefe");
		$where = "buss=".$buss_id;
		//共有多少条记录
		$count = $mp->where($where)->count();
		$page = new \Think\Page($count , $page->listRows);
		$page->parameter = $row; //此处的row是数组，为了传递查询条件
		$page->setConfig('first','首页');
		$page->setConfig('prev','上一页');
		$page->setConfig('next','下一页');
		$page->setConfig('last','尾页');
		$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% 第 '.I('p',1).' 页/共 %TOTAL_PAGE% 页 ( '.$pagecount.'共%TOTAL_ROW%条)');
		//获取优惠券信息列表
		$list = $mp->where($where)->order('id asc')->limit($page->firstRow.','.$page->listRows)->select();
		//判断是否开始
		$now = date('Y-m-d h:i:s');
		for ($i=0; $i <count($list) ; $i++) { 
			if ($now > $list[$i]['beg_time'] && $now < $list[$i]['end_time'] ) {
				$list[$i]['status'] = "正常";
			}else if($now > $list[$i]['end_time']){
				$list[$i]['status'] = "已结束";
			}else{
				$list[$i]['status'] = "未开始";
			}
		}
		$this->page = $page->show();
		//获取商店名字
		$dp = D('Prefe');
		$shopname = $dp->getShopName($buss_id);
		$this->assign('shopname',$shopname['shopname']);
		$this->assign('info',$list);
		$this->display();

	}
	//发布优惠
	public function addprefe(){
		// 如果有post数据过来，说明是提交表单的
		if (I("post.pname")) {
			//存保存到数据库的值
			$data = array();
			//如果有文件上传
			if($_FILES['image']['name']){
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     3145728 ;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath  =      './public/'; // 设置附件上传根目录
				// 上传单个文件 
				$info   =   $upload->uploadOne($_FILES['image']);
				if(!$info) {// 上传错误提示错误信息
				    $this->error($upload->getError());
				}else{// 上传成功 获取上传文件信息,存入data数组
					$filename = $info['savepath'].$info['savename'];
				    $st =   new Storage();
				    //获取url地址
					$data['image']=$st->getUrl("public", $filename);
				}
			}
			//把上传的其他信息写入数据库
			$data['name'] = I("post.pname");//这里叫pname为了html防止冲突(优惠名称)
			$data['type'] = I("post.type");//优惠类型
			$data['beg_time'] = I("post.act_start_time");
			$data['end_time'] = I("post.act_stop_time");
			$data['context'] = I("post.context");//优惠内容
			$data['buss'] = I("post.buss_id");
			$prefe = M("Prefe");
			//$sql = "insert"
			if($prefe->data($data)->add()){
				$this->success("添加成功");
			}else{
				$this->error("添加失败");
			}


		}else{
			$buss_id = session("admin_id");
			$this->assign('buss_id',$buss_id);
			$this->display();
		}	
	}
	//修改优惠界面
	public function editprefe(){
		if($id=I("get.id")){
			$mp = M("prefe");
			$where = "id=".$id;
			$list = $mp->where($where)->find();
			$this->assign("info",$list);
			$this->display();
		}
	}
	//修改优惠，并且上传图片
	public function setprefe(){
		//存保存到数据库的值
		$data = array();
		if($_FILES['image']['name']){
			//如果有文件上传
		   	$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->rootPath  =      './public/'; // 设置附件上传根目录
			// 上传单个文件 
			$info   =   $upload->uploadOne($_FILES['image']);
			if(!$info) {// 上传错误提示错误信息
			    $this->error($upload->getError());
			}else{// 上传成功 获取上传文件信息,存入data数组
				$filename = $info['savepath'].$info['savename'];
			    $st =   new Storage();
			    //获取url地址
				$data['image']=$st->getUrl("public", $filename);
			}
		}

		//把上传的其他信息写入数据库
		$data['type'] = I("post.type");
		$data['context'] = I("post.context");
		$data['name'] = I("post.pname");//这里叫pname为了html防止冲突

		$prefe = M("Prefe");
		if($prefe->where("id=".I("get.id"))->save($data)){
			$this->success("修改成功",U('Prefe/showprefe'));
		}else{
			$this->error("修改失败或未填入修改值");
		}
	}

}

